//
//  ManageTieZiHeaderView.m
//  Lucky28
//
//  Created by zr on 2018/8/2.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "ManageTieZiHeaderView.h"

@interface ManageTieZiHeaderView ()

@property (nonatomic, strong)UIButton * myselfButton;
@property (nonatomic, strong)UIButton * myCollectionButton;
@property (nonatomic, strong)UIButton * myLikeButton;
@property (nonatomic, strong)UIView * oneLineView;
@property (nonatomic, strong)UIView * twoLineView;
@property (nonatomic, strong)UIView * threeLineView;
@property (nonatomic, weak)UIView * bottomView;

@end

@implementation ManageTieZiHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
    }
    return self;
}


- (void)setupUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.myselfButton = [self setupButtonWith:@"我的帖子"];
    self.myselfButton.selected = YES;
    [self addSubview:self.myselfButton];
    
    self.myCollectionButton = [self setupButtonWith:@"我的收藏"];
    [self addSubview:self.myCollectionButton];
    
    self.myLikeButton = [self setupButtonWith:@"我的赞"];
    [self addSubview:self.myLikeButton];
    
    
    self.oneLineView = [self setupLineView];
    self.oneLineView.hidden = NO;
    [self addSubview:self.oneLineView];
    
    self.twoLineView = [self setupLineView];
    [self addSubview:self.twoLineView];
    
    self.threeLineView = [self setupLineView];
    [self addSubview:self.threeLineView];
    
    
    UIView * bottomView = [UIView new];
    
    bottomView.backgroundColor = ColorWithRGB(0xEEEEEE);
    
    self.bottomView = bottomView;
    
    [self addSubview:self.bottomView];
}



- (void)layoutSubviews{
    
    [self.myselfButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH / 3);
        make.leading.mas_equalTo(self);
        make.top.mas_equalTo(self).offset(10 * SCREENH6);
        make.height.mas_equalTo(15 * SCREENH6);
    }];
    
    [self.myCollectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.width.height.mas_equalTo(self.myselfButton);
    }];
    
    [self.myLikeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self);
        make.centerY.height.width.mas_equalTo(self.myCollectionButton);
    }];
    
    [self.oneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(50 * SCREENH6);
        make.height.mas_equalTo(4 * SCREENH6);
        make.centerX.mas_equalTo(self.myselfButton);
        make.bottom.mas_equalTo(self).offset(-7 * SCREENH6);
    }];
    
    [self.twoLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.centerY.mas_equalTo(self.oneLineView);
        make.centerX.mas_equalTo(self.myCollectionButton);
    }];
    
    [self.threeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.centerY.mas_equalTo(self.twoLineView);
        make.centerX.mas_equalTo(self.myLikeButton);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(self);
        make.height.mas_equalTo(5 * SCREENH6);
    }];
}


- (UIView *)setupLineView{
    
    UIView * lineView = [UIView new];
    
    lineView.hidden = YES;
    lineView.backgroundColor = MAINCOLOR;
    
    return lineView;
}

- (UIButton *)setupButtonWith:(NSString *)string{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:string forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    button.selected = NO;
    
    
    return button;
}


- (void)clickButton:(UIButton *)button{
    
    NSString * type;
    
    if (button == self.myselfButton) {
        
        if (!button.selected) {
            
            button.selected = YES;
            self.oneLineView.hidden = NO;
            
            self.myCollectionButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.myLikeButton.selected = NO;
            self.threeLineView.hidden = YES;
            
            type = @"0";
        }
    }else if (button == self.myCollectionButton){
        
        if (!button.selected) {
            
            button.selected = YES;
            self.twoLineView.hidden = NO;
            
            self.myselfButton.selected = NO;
            self.oneLineView.hidden = YES;
            self.myLikeButton.selected = NO;
            self.threeLineView.hidden = YES;
            
            type = @"2";
        }
    }else if (button == self.myLikeButton){
        
        if (!button.selected) {
            
            button.selected = YES;
            self.threeLineView.hidden = NO;
            
            self.myCollectionButton.selected = NO;
            self.twoLineView.hidden = YES;
            self.myselfButton.selected = NO;
            self.oneLineView.hidden = YES;
            
            type = @"3";
        }
    }
    
    NSDictionary * dict = @{@"type":type};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeTableViewData" object:nil userInfo:dict];
    
}

@end
